<template>
    <xh-pages>
        <view class="page flex flex-column flex-x-center">
            <view class="p-4 flex-2 flex flex-center">
                <!-- 申请开通 -->
                <view class="text-center" v-if="siteInfo.dealer_apply === '10'">
                    <u-icon name="lock" color="var(--xl-primary)" size="120" class="flex-center"/>
                    <view class="h1 font-weight-600 my-10">申请开通分销权限</view>
                    <view class="pt-4 h9 text-dark-light-5">申请开通分销权限后，您可以通过分享链接或二维码的方式推广本平台，推广成功后，您将获得相应的分销奖励
                    </view>
                    <view class="pt-10">
                        <!-- 付费开通 -->
                        <u-button type="primary" v-if="siteInfo.dealer.dealer_apply_price > 0" @click="applyDeaaler">
                            （￥{{ siteInfo.dealer.dealer_apply_price }}）申请开通
                        </u-button>
                        <!-- 免费开通 -->
                        <u-button type="primary" v-else @click="applyDeaaler">
                            立即申请开通
                        </u-button>
                    </view>
                </view>
                <!-- 等待审核 -->
                <view class="text-center" v-else-if="siteInfo.dealer_apply === '40'">
                    <u-icon name="lock" color="var(--xl-primary)" size="120" class="flex-center"/>
                    <view class="h1 font-weight-600 my-10">等待后台审核</view>
                    <view class="pt-4 h9 text-dark-light-5">申请开通分销权限后，您可以通过分享链接或二维码的方式推广本平台，推广成功后，您将获得相应的分销奖励
                    </view>
                    <!-- <view class="pt-4 h8 text-error font-weight-600"
                        v-if="dealer.config.dealer_apply_refund === 'no' && dealer.config.dealer_apply_price > 0">
                        {{dealer.config.dealer_apply_refund_text }}
                    </view> -->
                    <view class="pt-10">
                        <u-button type="primary">等待后台审核</u-button>
                    </view>
                </view>
                <!-- 已获得分销权限 -->
                <view class="text-center" v-else-if="siteInfo.dealer_apply === '20'">
                    <u-icon name="checkmark-circle" color="var(--xl-primary)" size="120" class="flex-center" />
                    <view class="h1 font-weight-600 my-10">已获得分销权限</view>
                    <view class="pt-4 h9 text-dark-light-5">申请开通分销权限后，您可以通过分享链接或二维码的方式推广本平台，推广成功后，您将获得相应的分销奖励
                    </view>
                </view>
                <!-- 分销权限冻结 -->
                <view class="text-center" v-else-if="siteInfo.dealer_apply === '30'">
                    <u-icon name="info-circle" color="var(--xl-primary)" size="120" class="flex-center" />
                    <view class="h1 font-weight-600 my-10">分销权限异常</view>
                    <view class="pt-4 h9 text-dark-light-5">当前分销权限已被冻结，你不可进行操作，请联系管理员</view>
                    <view class="pt-10">
                        <u-button type="error">权限异常</u-button>
                    </view>
                </view>
            </view>
            <view class="flex-1" />
        </view>
    </xh-pages>
</template>

<script lang="ts" setup>
import { storeToRefs } from 'pinia';
import { isWeiXinLogin, toast } from '@/utils/helpers';
import { useSite } from '@/stores/site';
import { useUserStore } from '@/stores/user';

const siteInfo = storeToRefs(useSite()).siteInfo
const userInfo = storeToRefs(useUserStore()).userInfo
const payInfo = ref();
const orderNo = ref()
const OrderStateNum = ref(1);

// 获取订单状态
const getOrderState = () => {
    uni.$u.http.get('Dealer/getOrderState', {
        params: {
            order_no: orderNo.value
        }
    }).then((res: any) => {
        if (res?.code === 200) {
            uni.hideLoading();
            toast(res?.msg ?? '网络错误')
        } else if (res?.code === 500) {
            uni.hideLoading();
            toast(res?.msg ?? '网络错误')
        } else{
            OrderStateNum.value++;
            if (OrderStateNum.value > 10) {
                uni.hideLoading();
                toast(res?.msg ?? '网络错误')
                return;
            }
            setTimeout(getOrderState, 1000)
        }
    })
}
// 公众号支付查询
const onBridgeReady = () => {    
    WeixinJSBridge.invoke('getBrandWCPayRequest', payInfo.value, (res: any) => {
        if (res.err_msg == "get_brand_wcpay_request:ok") {
            uni.showLoading({
                title: '查询中...',
                mask: true
            });
            getOrderState();
        }
    });
}

// 统一下单
const unifiedOrder = (order_no: string) => {
    const platform = isWeiXinLogin()&&userInfo.value.wechat_openid ? 'wechat' : 'h5';
    const params = { order_no, platform };
    uni.$u.http.get('Dealer/unifiedOrder', { params }).then((res: any) => {
        if (platform === 'wechat') {
            payInfo.value = res?.data
            if (typeof WeixinJSBridge == "undefined") {
                if (document.addEventListener) {
                    document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
                } else if (document.attachEvent) {
                    document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
                    document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
                }
            } else {
                onBridgeReady();
            }
        } else if (res?.data?.pay_url) {
            window.location.href = res.data.pay_url
        } else {
            toast(res?.msg ?? '网络错误')
        }
    }).catch((err:any) => {
        toast(err?.msg ?? '出现错误')
    })
}

// 提交申请分销
const applyDeaaler = () => {
    uni.$u.http.post('Dealer/apply').then((res: any) => {
        const order_no = res?.data?.order_no ?? '';
        if (order_no) {
            orderNo.value = order_no;
            unifiedOrder(order_no);
        } else {
            uni.$emit('site::update');
            uni.$emit('user::update');
            toast(res?.msg ?? '');
        }
    })
}
onShow(() => {
    uni.$emit('site::update');
})
</script>

<style lang="scss" scoped>
.page {
    height: calc(100vh - var(--window-top));
}
</style>
